package com.teemor.blog.service.impl;

import com.teemor.blog.bean.condition.TbFootprintCondition;
import com.teemor.blog.bean.entity.TbFootprint;
import com.teemor.blog.mapper.TbFootprintMapper;
import com.teemor.blog.service.IFootprintService;

import java.util.List;

import com.teemor.common.bean.utils.IPInfoUtils;
import com.teemor.common.bean.utils.enums.DataStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
  * @author: zhoulk
  * @Description: 原子服务实现
  * @date: 2020/03/22 10:55:25
 */
@Service
public class FootprintServiceImpl implements IFootprintService {

    @Autowired
    private TbFootprintMapper tbFootprintMapper;
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public int countFootprint(TbFootprintCondition condition) {
        return tbFootprintMapper.countFootprintByCondition(condition);
    }

    @Override
    public List<TbFootprint> qryFootprints(TbFootprintCondition condition) {
        condition.setDataState(DataStatus.USE.value());
        return tbFootprintMapper.selectFootprintByCondition(condition);
    }

    @Override
    public TbFootprint qryFootprintById(Integer id) {
        return tbFootprintMapper.selectFootprintById(id);
    }

    @Override
    public int addFootprint(TbFootprint inParam) {
        TbFootprintCondition condition = new TbFootprintCondition();
        condition.setAtcId(inParam.getAtcId());
        condition.setIpAddress(inParam.getIpAddress());
        int count = tbFootprintMapper.countFootprintByCondition(condition);
        //同一IP访问同一文章，只记录一次
        if (count <= 0) {
            inParam.setDataState(DataStatus.USE.value());
            inParam.setUserAddress(IPInfoUtils.getIpaddr(restTemplate, inParam.getIpAddress()));
            return tbFootprintMapper.insertFootprint(inParam);
        } else
            return 0;

    }

    @Override
    public int updFootprintById(TbFootprint inParam) {
        return tbFootprintMapper.updateFootprintById(inParam);
    }
}